package com.infindo.appcreate.zzyj.util;

import org.apache.log4j.Logger;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

public class Log4jListener implements BuildListener {
    Logger log = null;
    Logger rootLog = null;

    private final boolean initialized = true;

    public Log4jListener(Logger lg) {
    	log = lg;
    }

    public void buildStarted(BuildEvent event) {
        if (initialized) {
            log.info("Build started.");
        }
    }

    public void buildFinished(BuildEvent event) {
        if (initialized) {
            if (event.getException() == null) {
                log.info("Build finished.");
            } else {
                log.error("Build finished with error.", event.getException());
            }
        }
    }

    public void targetStarted(BuildEvent event) {
        if (initialized) {
            log.info("Target \"" + event.getTarget().getName() + "\" started.");
        }
    }

    public void targetFinished(BuildEvent event) {
        if (initialized) {
            String targetName = event.getTarget().getName();
            if (event.getException() == null) {
                log.info("Target \"" + targetName + "\" finished.");
            } else {
                log.error("Target \"" + targetName
                    + "\" finished with error.", event.getException());
            }
        }
    }

    public void taskStarted(BuildEvent event) {
        if (initialized) {
            Task task = event.getTask();
            log.info("Task \"" + task.getTaskName() + "\" started.");
        }
    }

    public void taskFinished(BuildEvent event) {
        if (initialized) {
            Task task = event.getTask();
            if (event.getException() == null) {
                log.info("Task \"" + task.getTaskName() + "\" finished.");
            } else {
                log.error("Task \"" + task.getTaskName()
                    + "\" finished with error.", event.getException());
            }
        }
    }

    public void messageLogged(BuildEvent event) {
        if (initialized) {
            Object categoryObject = event.getTask();
            if (categoryObject == null) {
                categoryObject = event.getTarget();
                if (categoryObject == null) {
                    categoryObject = event.getProject();
                }
            }

            switch (event.getPriority()) {
                case Project.MSG_ERR:
                    log.error(event.getMessage());
                    break;
                case Project.MSG_WARN:
                    log.warn(event.getMessage());
                    break;
                case Project.MSG_INFO:
                    log.info(event.getMessage());
                    break;
                case Project.MSG_VERBOSE:
                    log.debug(event.getMessage());
                    break;
                case Project.MSG_DEBUG:
                    log.debug(event.getMessage());
                    break;
                default:
                    log.error(event.getMessage());
                    break;
            }
        }
    }
}
